package com.violet.detective.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.violet.detective.entity.Defect;
import com.violet.detective.search.DefectSearchParameter;
import com.violet.detective.service.DefectService;
import com.violet.detective.mapper.DefectMapper;
import com.violet.detective.service.EquipmentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.stream.Collectors;

/**
* @author 21145
* @description 针对表【Defect】的数据库操作Service实现
* @createDate 2023-09-04 21:42:12
*/
@Service
public class DefectServiceImpl extends ServiceImpl<DefectMapper, Defect>
    implements DefectService{

    @Resource
    DefectMapper defectMapper;

    @Resource
    private EquipmentService equipmentService;

    public boolean addDefect(Defect defect) {
        int result = defectMapper.insert(defect);
        return result > 0;
    }

    public boolean editDefect(Defect updatedDefect) {
        int result = defectMapper.updateById(updatedDefect);
        return result > 0;
    }

    public boolean deleteDefect(Integer id) {
        int result = defectMapper.deleteById(id);
        return result > 0;
    }

    public Defect getDefectById(Integer id) {
        return defectMapper.selectById(id);
    }

    public List<Defect> searchDefects(DefectSearchParameter searchParameter) {
        QueryWrapper<Defect> query = new QueryWrapper<>();
        if (searchParameter.getEquipmentId() != null)  //设备编码
            query.eq("equipmentId", searchParameter.getEquipmentId());
        if (searchParameter.getState() != null)  //状态
            query.eq("state", searchParameter.getState());
        if (searchParameter.getUpdateTime() != null)  //更新时间
            query.ge("updateTime", searchParameter.getUpdateTime());
        if (searchParameter.getIsClosed() != null && searchParameter.getIsClosed())  // 是否关闭
            query.eq("state", "closed");
        if (searchParameter.getPhenomenon() != null)  //现象
            query.eq("phenomenon", searchParameter.getPhenomenon());
        if (searchParameter.getContent() != null)  //内容
            query.like("content", searchParameter.getContent());
        if (searchParameter.getEquipmentName() != null)
            query.like("equipmentName", searchParameter.getEquipmentName());
        if (searchParameter.getEquipmentMajor() != null)
            query.like("equipmentMajor", searchParameter.getEquipmentMajor());

        return defectMapper.selectList(query);
    }

    public Page<Defect> searchDefects(DefectSearchParameter searchParameter, int pageNumber, int pageSize) {
        System.out.println(searchParameter);
        Page<Defect> page = new Page<>(pageNumber, pageSize);
        QueryWrapper<Defect> query = new QueryWrapper<>();
        if (searchParameter.getEquipmentId() != null)  //设备编码
            query.eq("equipmentId", searchParameter.getEquipmentId());
        if (searchParameter.getState() != null)  //状态
            query.eq("state", searchParameter.getState());
        if (searchParameter.getUpdateTime() != null)  //更新时间
            query.ge("updateTime", searchParameter.getUpdateTime());
        if (searchParameter.getIsClosed() != null && searchParameter.getIsClosed())  // 是否关闭
            query.eq("state", "closed");
        if (searchParameter.getPhenomenon() != null)  //现象
            query.eq("phenomenon", searchParameter.getPhenomenon());
        if (searchParameter.getContent() != null)  //内容
            query.like("content", searchParameter.getContent());
        if (searchParameter.getEquipmentName() != null)
            query.like("equipmentName", searchParameter.getEquipmentName());
        if (searchParameter.getEquipmentMajor() != null)
            query.like("equipmentMajor", searchParameter.getEquipmentMajor());

        return defectMapper.selectPage(page, query);
    }


    public List<Defect> findAllDefects() {
        return defectMapper.selectList(null);
    }

    public Page<Defect> findAllDefects(int pageNum, int pageSize) {
        Page<Defect> page = new Page<>(pageNum, pageSize);
        return defectMapper.selectPage(page,null);
    }

}




